from selenium import webdriver
import unittest
import os
import time
from public.url import wkurl
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec

class TestDenglu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        url=wkurl(self.driver).test_url()
        self.driver.get(url+"/index.html#/login?redirect=%2F404")
        self.driver.maximize_window()
        time.sleep(5)
        print("starttime:" + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))

    def tearDown(self):
        filedir = "D:/test/screenshot/"
        if not os.path.exists(filedir):
            os.makedirs(os.path.join('D:/', 'test', 'screenshot'))
        print("endTime:" + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())))
        screen_name = filedir + time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) + ".png"
        self.driver.get_screenshot_as_file(screen_name)
        self.driver.quit()

    def testdunlu01(self):
        '''使用已存在账号密码成功登录'''
        self.driver.find_element_by_name('username').send_keys('15770820028')
        self.driver.find_element_by_name('password').send_keys('123456')
        time.sleep(2)
        self.driver.find_element_by_xpath('//button[@type="button"]').click()
        time.sleep(10)

        bangong=self.driver.find_element_by_link_text('办公').text
        kehugl=self.driver.find_element_by_link_text('客户管理').text
        syzn=self.driver.find_element_by_link_text('商业智能').text

        self.assertEqual('办公',bangong)
        self.assertEqual('客户管理',kehugl)
        self.assertEqual('商业智能',syzn)

    def testdunlu_02(self):
        '''使用不存在的账号登录失败并提示'''
        self.driver.find_element_by_name('username').send_keys('15770820')
        self.driver.find_element_by_name('password').send_keys('51284046')
        time.sleep(2)
        self.driver.find_element_by_xpath('//button[@type="button"]').click()

        tishi=WebDriverWait(self.driver,10,0.1).until(ec.presence_of_element_located((By.XPATH,'//div[@role="alert"]/p'))).text

        self.assertEqual('用户名或密码错误！', tishi)

if __name__ == "__main__":
    unittest.main()



